# -*- coding:utf-8 -*-
from tornado import gen
import os

from appcommon.framework import WebBaseHandler
from common import utiltool
from common import timetool
import models


class CircleListHandler(WebBaseHandler):
    """
    话题列表
    """
    @gen.coroutine
    def post(self):
        type = self.get_argument('type', 'all')
        c_id = self.get_argument('c_id', '')
        page_count = self.get_argument('page_count', '20')
        info = yield models.get_circle_list(c_id, page_count, type, 0)
        for item in info:
            reply_cnt = yield models.get_reply_cnt(item['c_id'])
            zan_cnt = yield models.get_zan_cnt(item['c_id'])
            item['is_silence'] = yield models.check_user_silence(item['phone'])
            item['reply_cnt'] = reply_cnt
            item['zan_cnt'] = zan_cnt

        res = {"code": "0", "info": info, "last_id": info[-1]['c_id'] if info else 0}
        utiltool.response_write(self, res)


class CouchCircleListHandler(WebBaseHandler):
    """
    教练圈话题列表
    """
    @gen.coroutine
    def post(self):
        type = self.get_argument('type', 'all')
        c_id = self.get_argument('c_id', '')
        page_count = self.get_argument('page_count', '20')
        info = yield models.get_circle_list(c_id, page_count, type, 1)
        for item in info:
            reply_cnt = yield models.get_reply_cnt(item['c_id'])
            zan_cnt = yield models.get_zan_cnt(item['c_id'])
            item['is_silence'] = yield models.check_user_silence(item['phone'])
            item['reply_cnt'] = reply_cnt
            item['zan_cnt'] = zan_cnt

        res = {"code": "0", "info": info, "last_id": info[-1]['c_id'] if info else 0}
        utiltool.response_write(self, res)


class CircleStatusHandler(WebBaseHandler):
    """
    话题列表
    """
    @gen.coroutine
    def post(self):
        c_id = self.get_argument('c_id', '')
        is_using = self.get_argument('is_using', '1')
        yield models.change_circle_status(c_id, is_using)
        res = {"code": "0", "msg": u"修改成功"}
        utiltool.response_write(self, res)


class CircleTopHandler(WebBaseHandler):
    """
    置顶
    """
    @gen.coroutine
    def post(self):
        c_id = self.get_argument('c_id', '')
        is_top = self.get_argument('is_top', '1')
        yield models.change_circle_top(c_id, is_top)
        res = {"code": "0", "msg": u"修改成功"}
        utiltool.response_write(self, res)


class ReplylistHandler(WebBaseHandler):
    """
    回复列表
    """
    @gen.coroutine
    def post(self):
        print self.request.arguments
        c_id = self.get_argument('c_id', '5')
        reply_id = self.get_argument('reply_id', '')
        page_count = self.get_argument('page_count', '20')
        info = yield models.get_reply_list(c_id, page_count, reply_id)

        res = {"code": "0", "info": info, "last_id": info[-1]['reply_id'] if info else 0}
        utiltool.response_write(self, res)


class ReplyStatusHandler(WebBaseHandler):
    """
    屏蔽回复
    """
    @gen.coroutine
    def post(self):
        reply_id = self.get_argument('reply_id', '')
        is_using = self.get_argument('is_using', '1')
        yield models.change_reply_status(reply_id, is_using)
        res = {"code": "0", "msg": u"修改成功"}
        utiltool.response_write(self, res)


class SetSilenceHandler(WebBaseHandler):
    """
    禁言
    """
    @gen.coroutine
    def post(self):
        current_time = timetool.get_timestamp()
        phone = self.get_argument('phone', '')
        is_silence = self.get_argument('is_silence', '1')
        yield models.set_silence(phone, current_time, is_silence)
        res = {"code": "0", "msg": u"修改成功"}
        utiltool.response_write(self, res)
